package com.jixu.streetapp.controller;


import com.jixu.streetapp.common.R;
import com.jixu.streetapp.entity.Street;
import com.jixu.streetapp.excel.StreetExcel;
import com.jixu.streetapp.service.StreetService;
import com.jixu.streetapp.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author "Jixu" 664480310@qq.com
 * @since 1.0.0 2023-02-20
 */
@RestController
@RequestMapping("/street")
@Slf4j
public class StreetController {
    @Autowired
    private StreetService streetService;
    private String filePath = "C:\\Users\\aaa\\Desktop\\街道信息.xlsx";

    @PostMapping("/importExcel")
    public R<String> importExcel(@RequestParam("file") MultipartFile file) throws IOException {

        streetService.importExcel(file);
        List<Street> list = streetService.list();
        List<StreetExcel> streetExcels = list.stream().map((item) -> {
            StreetExcel streetExcel = new StreetExcel();
            BeanUtils.copyProperties(item, streetExcel);
            return streetExcel;
        }).collect(Collectors.toList());
        ExcelUtil.writeWithTemplate(filePath, streetExcels);

        return R.success("success");
    }

}